package www.lagou.app;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import www.lagou.utils.DruidUtils;

import java.sql.SQLException;
import java.util.List;

public class TestPhone {

    public static void main(String[] args) {
        //1.获取自动模式QueryRunner()
        QueryRunner queryRunner = new QueryRunner(DruidUtils.dataSource);

        //2.编写sql
        //需求1:  查询价格高于2000元，生产日期是2019年之前的所有手机
        String sql = "SELECT * FROM phone WHERE price > 2000 AND prodate < '2019-01-01';";
        //需求2:  查询所有颜色是白色的手机信息
        String sql2 = "SELECT * FROM phone WHERE color = '白色';";
        //3.执行sql
        try {
            List<Object[]> query = queryRunner.query(sql, new ArrayListHandler());

            //打印返回的结果
            for (Object[] objects : query) {
                for (Object object : objects) {
                    System.out.print("\t" + object.toString());
                }
                System.out.println();
            }

            System.out.println("-------------------");
            List<Object[]> query2 = queryRunner.query(sql2, new ArrayListHandler());
            //打印返回的结果
            for (Object[] objects : query2) {
                for (Object object : objects) {
                    System.out.print("\t" + object.toString());
                }
                System.out.println();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}
